﻿Imports IWshRuntimeLibrary

Module Module1
    Sub Main()
        Console.WriteLine("Welcome to the Startup Manager Installer")
        Console.WriteLine("Press any key to continue or close this window to exit")
        Console.ReadLine()

        Console.Clear()

        My.Computer.Registry.LocalMachine.OpenSubKey("Software\", True).CreateSubKey("Startup Manager")
        My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).CreateSubKey("Managed Startup")
        My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).CreateSubKey("Managed Startup")
        My.Computer.Registry.CurrentUser.OpenSubKey("Software\", True).CreateSubKey("Startup Manager")

        Try
            If System.IO.File.Exists("C:\Program Files\Startup Manager\Startup Manager Service.exe") = True Then
                Console.WriteLine("Trying to stop Startup Manager Service")
                Try
                    Dim proc As Process = Diagnostics.Process.GetProcessesByName("Startup Manager Service")(0)
                    proc.Kill()
                    Try
                        proc.WaitForExit()
                    Catch
                    End Try
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

        Try
            Try
                System.IO.Directory.CreateDirectory("C:\Program Files\Startup Manager")
                Console.WriteLine("Created Directory")
            Catch
            End Try
            Dim path As String = System.Reflection.Assembly.GetExecutingAssembly().Location
            path = path.Remove(path.Length - 13)

            For Each file In System.IO.Directory.GetFiles(path)
                Dim filename As String = file.Remove(0, path.Length)
                If Not filename.StartsWith("Installer") Then
                    Console.WriteLine("Moving " & filename)
                    System.IO.File.Copy(path & filename, "C:\Program Files\Startup Manager\" & filename, True)
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        Try
            System.IO.Directory.CreateDirectory("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup Manager")
        Catch
        End Try

        Try
            Console.WriteLine("Creating Shortcuts")

            Dim shell As WshShell = New WshShellClass
            Dim sm As WshShortcut = shell.CreateShortcut("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup Manager\Startup Manager.lnk")
            sm.TargetPath = "C:\Program Files\Startup Manager\Startup Manager.exe"
            sm.Description = "Launches Startup Manager"
            sm.Save()

            Dim un As WshShortcut = shell.CreateShortcut("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup Manager\Uninstaller.lnk")
            un.TargetPath = "C:\Program Files\Startup Manager\Uninstaller.exe"
            un.Description = "Uninstalls Startup Manager"
            un.Save()

            Dim aa As WshShortcut = shell.CreateShortcut("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup Manager\AAUSP.lnk")
            aa.TargetPath = "C:\Program Files\Startup Manager\AAUSP.exe"
            aa.Description = "Launches AAUSP"
            aa.Save()

            Dim sof As WshShortcut = shell.CreateShortcut("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup Manager\Change Startup Order.lnk")
            sof.TargetPath = "C:\Program Files\Startup Manager\Startup Manager.exe"
            sof.Arguments = "/O"
            sof.Save()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Console.Clear()
        If Debugger.IsAttached = False Then
            Console.WriteLine("Running Startup Manager")

            Dim Proc As New Process
            Dim ProcStartInfo As New ProcessStartInfo("C:\Program Files\Startup Manager\Startup Manager.exe")
            ProcStartInfo.UseShellExecute = True
            Proc.StartInfo = ProcStartInfo
            Proc.Start()

            Console.WriteLine("Please Wait...")
            Proc.WaitForExit()

            Console.WriteLine("Running Startup Manager Service")

            Proc = New Process
            ProcStartInfo = New ProcessStartInfo("C:\Program Files\Startup Manager\Startup Manager Service.exe")
            ProcStartInfo.UseShellExecute = True
            Proc.StartInfo = ProcStartInfo
            Proc.Start()
        Else
            Console.WriteLine("Would start AAUSP and Startup Manager but debugger is attached.")
        End If

        Console.WriteLine("Finished Installing Startup Manager")
        Console.WriteLine("Press any key to exit")
        Console.ReadLine()
    End Sub

End Module
